package org.inspursc.s0517.health.common.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.inspursc.s0517.health.common.converter.OrgConverter2OrgDTO;
import org.inspursc.s0517.health.common.dto.SysOrganizationDTO;
import org.inspursc.s0517.health.common.enums.ResultEnum;
import org.inspursc.s0517.health.common.exception.BizException;
import org.inspursc.s0517.health.common.form.OrganizationForm;
import org.inspursc.s0517.health.common.service.SysOrganizationService;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author B_Dream
 * @date 2018/6/7 13:54
 */
@Api(description = "机构组织控制Api" )
@RestController
@RequestMapping(value = "/organization")
public class SysOrganizationController {

    @Autowired
    private SysOrganizationService orgService;

    @ApiOperation(value = "保存机构组织信息")
    @PostMapping("/save")
    public ResponseEntity<ResponseVO> save(@RequestBody @Valid OrganizationForm orgForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        SysOrganizationDTO oldOrgDTO = OrgConverter2OrgDTO.converter(orgForm);
        SysOrganizationDTO orgDTO = orgService.save(oldOrgDTO);
        if (null != orgDTO) {
            return ResponseUtil.success("保存成功", orgDTO);
        }
        return ResponseUtil.error("保存失败", orgDTO);
    }

    @ApiOperation(value = "根据机构组织ID，删除机构组织信息")
    @GetMapping("/delete/{id}")
    public ResponseEntity<ResponseVO> delete(@ApiParam(value = "机构组织ID", required = true) @PathVariable Integer id) {
        boolean is = orgService.deleteById(id);
        if (is) {
            return ResponseUtil.success("删除成功", is);
        }
        return ResponseUtil.error("删除失败", is);
    }


    @ApiOperation(value = "根据机构组织ID，获取机构组织信息")
    @GetMapping("/get/{id}")
    public ResponseEntity<ResponseVO> get(@ApiParam(value = "机构组织ID", required = true) @PathVariable Integer id) {

        SysOrganizationDTO role = orgService.findOneById(id);
        if (null != role) {
            return ResponseUtil.success("", role);
        }
        return ResponseUtil.error("机构组织信息为空",null);
    }

    @ApiOperation(value = "根据域名，获取机构组织信息")
    @GetMapping("/getByDomainName")
    public ResponseEntity<ResponseVO> getByDomainName(@ApiParam(value = "根据域名获取组织ID", required = true) @RequestParam String domainName) {

        SysOrganizationDTO so = orgService.findOneByDomainName(domainName);
        if (null != so) {
            return ResponseUtil.success("", so);
        }
        return ResponseUtil.success("ok",null);
    }



    @ApiOperation(value = "获取所有机构组织信息")
    @GetMapping(value = "/getAll")
    public ResponseEntity<ResponseVO> getAll() {
        List<SysOrganizationDTO> dtoList = orgService.findAll();
        return ResponseUtil.success("", dtoList);
    }




}
